package com.ghost.alibabaghost.service.sys;

import com.ghost.alibabaghost.api.service.sys.SysMenusService;
import com.ghost.alibabaghost.api.model.sys.SysMenusModel;
import com.ghost.alibabaghost.dao.dataobject.sys.SysMenusDO;
import com.ghost.alibabaghost.dao.mapper.sys.SysMenusMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/15 08:29
 */
@Service("sysMenusService")
public class SysMenusServiceImpl implements SysMenusService {

    @Autowired
    private SysMenusMapper sysMenusMapper;

    @Override
    public List<SysMenusModel> getNodeTree() {
        List<SysMenusDO> nodeTree = sysMenusMapper.getNodeTree();
        List<SysMenusModel> collect = nodeTree.stream().map(sysMenusDO -> {
            SysMenusModel sysMenusModel = new SysMenusModel();
            BeanUtils.copyProperties(sysMenusDO, sysMenusModel);
            return sysMenusModel;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Integer updateMenu(SysMenusModel sysMenusModel) {
        SysMenusDO sysMenusDO = new SysMenusDO();
        BeanUtils.copyProperties(sysMenusModel,sysMenusDO);
        return sysMenusMapper.updateMenu(sysMenusDO);
    }

    @Override
    public Integer insertMenu(SysMenusModel sysMenusModel) {
        SysMenusDO sysMenusDO = new SysMenusDO();
        BeanUtils.copyProperties(sysMenusModel,sysMenusDO);
        return sysMenusMapper.insertMenu(sysMenusDO);
    }

    @Override
    public SysMenusModel queryByNodeId(String nodeId) {
        SysMenusDO sysMenusDO =  sysMenusMapper.queryByNodeId(nodeId);
        SysMenusModel sysMenusModel = new SysMenusModel();
        BeanUtils.copyProperties(sysMenusDO,sysMenusModel);
        return sysMenusModel;
    }
}
